package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.interceptor.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class ReggieWebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor;

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        //自定义放行的路径
        ArrayList<String> urls = new ArrayList<>();
        urls.add("/front/**");
        urls.add("/error");//错误请求
        urls.add("/user/sendMsg");//验证码发送
        urls.add("/user/login");//登录
        urls.add("/user/logout");//退出

        registry.addInterceptor(loginCheckInterceptor)
                .addPathPatterns("/**")//拦截所有
                .excludePathPatterns(urls);//放行指定路径


    }
    //静态资源映射
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/front/**")
                .addResourceLocations("classpath:/front/");

    }
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters){
        //消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //底层使用Jackson将java转为json
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        converters.add(0,messageConverter);

    }
}
